package com.epam.traning.taskmanager;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.epam.traning.taskmanager.auth.VkOAuthHelper;

public class VkLoginActivity extends Activity {
	
	private static final String TAG = VkLoginActivity.class.getSimpleName();

	private WebView mWebView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vk_login);
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.setWebViewClient(new VkWebViewClient());
		mWebView.loadUrl(VkOAuthHelper.AUTORIZATION_URL);
		
    }
    
    private class VkWebViewClient extends WebViewClient {

    	/**
		 * Instantiates a new twitter login web view client.
		 *
		 * @param dialog the dialog
		 * @param runnable the runnable
		 */
		public VkWebViewClient() {
			super();
			
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			Log.d(TAG, "page started " + url);
			showLoading();
			if (VkOAuthHelper.isRedirect(url)) {
				view.setVisibility(View.INVISIBLE);
				Log.d(TAG, "Parsing url" + url);
			}
		}

		
		
		private void showLoading() {
			findViewById(R.id.loading).setVisibility(View.VISIBLE);
		}

		/* (non-Javadoc)
		 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
		 */
		@Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.d(TAG, "overr " + url);
	        if (VkOAuthHelper.isRedirect(url)) {
	        	Log.d(TAG, "overr redr");
	        	view.setVisibility(View.INVISIBLE);
	        	Log.d(TAG, "Parsing url" + url);
	        	return true;
	        } else {
	        	 //view.loadUrl(url);
	        	 return false;
	        }
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			//showLoadingDialog("Error: " + description);
			Log.d(TAG, "error " + failingUrl);
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			Log.d(TAG, "finish " + url);
			if (url.contains("&amp;")) {
				url = url.replace("&amp;", "&");
				Log.d(TAG, "overr after replace " + url);
				view.loadUrl(url);
				return;
			}
			if (!VkOAuthHelper.proceedRedirectURL(VkLoginActivity.this, url, success)) {
				hideLoading();
			}
		}

		private void hideLoading() {
			findViewById(R.id.loading).setVisibility(View.GONE);
		}

	}
    
    private Runnable success = new Runnable() {
		
		@Override
		public void run() {
			setResult(RESULT_OK);
			VkLoginActivity.this.finish();
		}
		
	};
    
}